<?php
defined('BY_JYA') or exit('error');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['group_id']){
		$params['group_id']=$_RQ['group_id'];
		$return['params']['group_id']=$_RQ['group_id'];
	}
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby=array();
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['num']){
		$psize = $_RQ['num'];
	}else{
		$psize = 10;
	}
	$total='';
	$list=pdo_getslice('jy_wenjstar_list',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['desc'] = $item['desc'] ? $item['desc'] : cfc('tools')->substr($item['content'],0,36);			
			$item['url']=custom_url('jy_wenjstar/diaocha//id='.$item['id'],true);
		}
	}
	$return['list']=$list?$list:array();
	$return['group']=$group?$group:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'问卷列表'),
			array('op'=>'edit','title'=>'添加问卷'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：选择题库分类，自动绑定相同题库的题目！','list'=>array(
			array('name'=>'title','title'=>'问卷标题','type'=>'string'),
			array('name'=>'group_id','title'=>'关联题库','type'=>'select','options'=>'groups'),
			array('name'=>'readnum','title'=>'阅卷量','type'=>'string'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'status','title'=>'状态','type'=>'switch','set'=>'set'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'export','action'=>'/list/export/','title'=>'导出问卷'),
			array('type'=>'href','op'=>'settiids','title'=>'设置题目'),
		)),
		'search'=>array(
			array('name'=>'title','width'=>10,'title'=>'问卷标题','type'=>'string'),
			array('name'=>'group_id','width'=>10,'title'=>'关联题库','type'=>'select','options'=>'groups'),
		),
	);
	$return['groups']=$this->getall('group',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='set'){
	$post=get_postdata();
	$id=intval($_RQ['id']);
	pdo_update('jy_wenjstar_list',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	exi('操作成功！');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_wenjstar_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_wenjstar_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='edit'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_wenjstar_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		$item['diyformfields'] =json_decode($item['diyformfields'],true);
	}else{
		$item=array('status'=>1);
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回问卷列表'),
		),
		'edit'=>array(
			array('name'=>'title','title'=>'名称','type'=>'string','must'=>true),
			array('name'=>'image','title'=>'logo','type'=>'image'),
			array('name'=>'descimage','title'=>'描述图','type'=>'image'),
			array('name'=>'desc','title'=>'描述','type'=>'richtext'),
			array('name'=>'enddesc','title'=>'END感谢语','type'=>'richtext'),
			array('name'=>'group_id','title'=>'关联题库','type'=>'select','options'=>'groups'),
			array(
				'type'=>'array',
				'name'=>'diyformfields',
				'title'=>'客户信息',
				'add_tip'=>'添加信息项',
				'list'=>array(
					array('type'=>'string','name'=>'title','title'=>'标题','help'=>''),
					array('type'=>'radio','name'=>'type','title'=>'类型','options'=>'types'),
					array('type'=>'string','name'=>'placeholder','title'=>'提示语'),
					array('type'=>'switch','name'=>'ismust','title'=>'*必填?'),							
				),
				'details'=>array(
					array('type'=>'string','name'=>'title','title'=>'标题','help'=>''),
					array('type'=>'radio','name'=>'type','title'=>'类型','options'=>'types'),
					array(
						'type'=>'array_simple',
						'show'=>array('type'=>array(4,5,17,19)),
						'name'=>'value',
						'add_tip'=>'添加自定义表单项',
						'fields'=>array(
							array('type'=>'string','name'=>'title','title'=>'选项名称','help'=>''),
						),
					),
					array('type'=>'string','name'=>'placeholder','title'=>'提示语'),
					array('type'=>'switch','name'=>'ismust','title'=>'*必填?'),	
				)
			),
			array('name'=>'needdesc','title'=>'答题弹窗？','type'=>'switch'),
			array('name'=>'status','title'=>'状态','type'=>'switch'),
		)
	);
	$return['groups']=pdo_getall('jy_wenjstar_group',array('uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	$return['types']=$this->formtype;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	$fields=array('title','image','descimage','tiids','times','group_id','desc','status','enddesc','needdesc');
	$data=array_fields($fields,$postdata);
	$data['diyformfields']=ijson_encode($postdata['diyformfields']);
	if(empty($data['title'])){
		exi('请填写问卷标题！','error');
	}
	if(empty($data['group_id'])){
		exi('请绑定题库！','error');
	}
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_wenjstar_list',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		exi('编辑问卷成功！','','goback');
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['createtime']=TIMESTAMP;
		pdo_insert('jy_wenjstar_list',$data);
		$id=pdo_insertid();
		exi('添加问卷成功！','','list');
	}
}
if($_RQ['op']=='settiids'){
	$return=array();
	if($_RQ['postdata']){
		$post=get_postdata();
		$update=array(
			'tiids'=>$post['tiids'],
		);
		pdo_update('jy_wenjstar_list',$update,array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['id']));
		exi('设置成功！','','goback');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_wenjstar_list',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		exi('请选择问卷！','error');
	}
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回问卷列表'),
		),
		'edit'=>array(
			array('type'=>'checkbox','name'=>'tiids','options'=>'tilist','title'=>'选择题目'),
		)
	);
	$tilist=pdo_getall('jy_wenjstar_tiku',array('group_id'=>$item['group_id'],'uniacid'=>$_SESSION['uniacid']),array('title','id'),'id');
	foreach($tilist as $tv){
		$return['tilist'][$tv['id']]=$tv['title'];
	}
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='export'){
	if(empty($_RQ['id'])){
		exi('请选择问卷！','error');
	}
	$params=array('uniacid'=>$_SESSION['uniacid'],'listid'=>$_RQ['id']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$this->model('diaocha')->down_csv($params);
}
